package psi.vote.view.service;

import model.Party;
import model.Voter;
import org.codehaus.jackson.map.ObjectMapper;
import psi.vote.view.interfaces.IVoteSystem;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;


/**
 * Created by user on 12.10.2014.
 */
public class VoteSystemRest implements IVoteSystem {

    public boolean isAlreadyVote(String userId) {
        String res = null;
        try {
            res = sendGet("http://localhost:8080/vote/check/" + userId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(res.equals("true")) {
            return true;
        } else {
            return false;
        }
    }

    public boolean vote(String userId, int partyId) {
        String res = "false";   //http://localhost:8080/vote/userId/tverdokhliebov@mail/partyId/3
        try {
            res = sendGet("http://localhost:8080/vote/userId/" + userId + "/partyId/" + partyId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(res.equals("true")) {
            return true;
        } else {
            return false;
        }
    }

    public List<Party> getAllParties(int page){
        String res = null;
        try {
            res = sendGet("http://localhost:8080/vote/list/"+ page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        List<Party> list = null;
        try {
            list = objectMapper.readValue(res, objectMapper.getTypeFactory().constructCollectionType(
                    List.class, Party.class
            ));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    public Voter getVoter(String userId) {
        String res = null;
        Voter voter = null;
        try {
            res = sendGet("http://localhost:8080/vote/voter/userId/"+ userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Res = " + res);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            voter = objectMapper.readValue(res, Voter.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return voter;
    }

    private String sendGet(String url) throws Exception {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        // optional default is GET
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    }

    // HTTP POST request
    private String sendPost(String url, String data) throws Exception {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        //add reuqest header
        con.setRequestMethod("POST");
        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(data);
        wr.flush();
        wr.close();
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return response.toString();
    }
}